import enum


class MaestroErrorCode:
    def __init__(self, code, message):
        self.code = code
        self.message = message


# 预定义常见错误类型
ILLEGAL_PARAMETER = MaestroErrorCode(1, '参数错误')
ACCESS_DENIED = MaestroErrorCode(2, '权限不足')
VERIFY_CODE_ERROR = MaestroErrorCode(3, '验证码错误')
ERROR_USER_NOT_EXISTS = MaestroErrorCode(1001, '用户不存在')
ERROR_USER_PASSWORD_INCORRECT = MaestroErrorCode(1002, '密码不正确')
ERROR_USER_DISABLED = MaestroErrorCode(1004, '用户已被禁用')
ERROR_USER_NAME_DUPLICATE = MaestroErrorCode(1006, '用户名已存在')
ERROR_PERMISSION_NOT_AUTHORIZED = MaestroErrorCode(1202, '未授权操作')

SERVER_ERROR = MaestroErrorCode(3000, '服务异常')
DATA_ERROR = MaestroErrorCode(3001, "数据异常")


class ExceptionType(enum.Enum):
    REGULAR = 'REGULAR'
    TG_ALARM = 'TG_ALARM'


class MaestroException(Exception):
    def __init__(self, error_code, message=None, exception_type=ExceptionType.REGULAR):
        assert isinstance(error_code, MaestroErrorCode)
        assert isinstance(exception_type, ExceptionType)
        self.error_code = error_code
        _msg = error_code.message
        if message:
            _msg = '{} - {}'.format(error_code.message, message)
        self.detail_message = _msg
        super().__init__('code: {}, message: {}, exception_type: {}, '.format(
            error_code.code, _msg, exception_type.value))
